package cn.sinata.wanglin.mine.activity;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.StringUtils;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.ResultData;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.MySwitchPasswordEditText;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * 忘记密码界面
 */
public class ForgetPwdActivity extends TitleActivity {
    @BindView(R.id.tv_getCode)
    TextView tv_getCode;//获取短信验证码
    @BindView(R.id.spet_password)
    MySwitchPasswordEditText spet_password;//密码的输入布局
    @BindView(R.id.et_phone)
    EditText et_phone;//手机号输入框
    @BindView(R.id.et_code)
    EditText et_code;//短信验证码输入框
    private boolean isTimerStart=false;
    @Override
    protected int setContentId() {
        return R.layout.activity_forget_pwd;
    }
    @Override
    protected void initView() {
        super.initView();
        setTitle("找回密码");
    }

    @OnClick({R.id.tv_getbackpwd,R.id.tv_getCode})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_getCode:
                //描述:获取验证码。
                sendSMSCode();
                break;
            case R.id.tv_getbackpwd:
                //描述:确定操作
                alterPwd();
                break;
        }
    }

    /**
     * 确认修改
     */
    private void alterPwd() {
        String phone = et_phone.getText().toString().trim();
        String code = et_code.getText().toString().trim();
        String password = spet_password.getText();
        if (TextUtils.isEmpty(phone)){
            showToast("手机号不能为空!");
            return;
        }
        if (TextUtils.isEmpty(code)){
            showToast("验证码不能为空!");
            return;
        }
        if (TextUtils.isEmpty(password)){
            showToast("密码不能为空!");
            return;
        }
        if(phone.length() != 11|| !StringUtils.isPhoneNumberValid(phone) ){
            showToast("请输入正确的手机号!");
            return;
        }
        if(code.length() != 6){
            showToast("请输入6位验证码!");
            return;
        }
        if(password.length() < 6||password.length()>20){
            showToast("请输入6-20位密码!");
            return;
        }

        alterPwd(phone,code,password);
    }

    private void alterPwd(String phone, String code, final String password) {
        Observable<ResultData<JsonObject>> observable = HttpManager.alterPwd(phone,code,password);
        observable.subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        showDialog();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        SharedPreferencesUtils.save(Constant.SP_USER_PWD,password);
                        finish();
                    }
                });
    }

    @OnTextChanged(value = R.id.et_phone,callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void afterPhoneTextChanged(Editable s){
        if (!TextUtils.isEmpty(s) && s.length() == 11 && StringUtils.isPhoneNumberValid(s.toString()) ){
            if (!isTimerStart) {
                tv_getCode.setEnabled(true);
            }
        }else {
            tv_getCode.setEnabled(false);
        }
    }

    /**
     * 获取短信验证码
     */
    private void sendSMSCode(){
        String phone = et_phone.getText().toString().trim();
        Observable<ResultData<JsonObject>> observable = HttpManager.sendCodeSms(phone,"D");
        observable.subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        showDialog();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showToast(msg);
                        timer.start();
                        isTimerStart = true;
                        tv_getCode.setEnabled(false);
                    }
                });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timer!=null) {
            timer.cancel();
        }
    }
    private CountDownTimer timer = new CountDownTimer(60*1000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            if (isDestroy){
                return;
            }
            tv_getCode.setText(String.format("%s",(millisUntilFinished/1000)));
        }

        @Override
        public void onFinish() {
            tv_getCode.setText("获取验证码");
            isTimerStart = false;
            tv_getCode.setEnabled(true);
        }
    };
}
